
import {
    toast
} from 'nc-lightapp-front';

import saveShowOrder from '../../request-functions/saveShowOrder';
import sendemail from '../../request-functions/sendemail';

import language from '../../language';

class AdjustSort  {
    constructor(props) {
        this.props = props;

        this.showAdjustPage = this.showAdjustPage.bind(this);
        this.closeAdjustPage = this.closeAdjustPage.bind(this);
        this.saveShowOrder = this.saveShowOrder.bind(this);
        this.sendemail = this.sendemail.bind(this);
    }

    showAdjustPage() {
        const {
            updateState,
            asyncTree,
            editTable,
            setButtonStatus2
        } = this.props;

        updateState({
            showTableCheckbox: false,
            page: 'adjust-sort'
        });
        asyncTree.setAsyncNodeDisable('departmentTree', true);
        // setButtonStatus('adjust-sort');
        setButtonStatus2('adjustSort');
        editTable.hideColByKey('ncc600704list', 'operation');
        editTable.showColByKey('ncc600704list', 'showorder');
        editTable.setStatus('ncc600704list', 'edit');
    }

    closeAdjustPage() {
        const {
            updateState,
            asyncTree,
            editTable,
            setButtonStatus2
        } = this.props;

        updateState({
            showTableCheckbox: true,
            page: 'main'
        });
        asyncTree.setAsyncNodeDisable('departmentTree', false);
        // setButtonStatus('main');
        setButtonStatus2('mainHadData');
        editTable.showColByKey('ncc600704list', 'operation');
        editTable.hideColByKey('ncc600704list', 'showorder');
        editTable.setStatus('ncc600704list', 'browse');
        this.props.goBackToMainPage();
    }

    async saveShowOrder() {
        const {humanResourceOrganizationValue} = this.props;

        let data = this.props.editTable.getAllRows('ncc600704list', true);
        let postData = {
            pk_org: humanResourceOrganizationValue.refpk,
            param_json: []
        };

        data.map((item) => {
            postData.param_json.push({
                pk_psnjob: item.values.pk_psnjob.value,
                showorder: item.values.showorder.value
            });
        });

        try {
            let res = await saveShowOrder(postData);
            
            if(res.success) {
                toast({
                    color: 'success',
                    content: language['hi6007-000000']/* 国际化处理： 保存成功*/
                });
                this.closeAdjustPage();
            }
        }
        catch(e) {

        }
    }
    // 发邮件
    async sendemail() {
        debugger
        const {humanResourceOrganizationValue} = this.props;

        let data = this.props.editTable.getCheckedRows('ncc600704list', true);
        let postData = {
        };
        let pk_psndoc = [];
        data.map((item) => {
            if (item && item.data && item.data.values && item.data.values.pk_psndoc && item.data.values.pk_psndoc.value) {
                pk_psndoc.push(item.data.values.pk_psndoc.value);
            }
        });
        if (pk_psndoc.length == 0) {
            toast({
                color: 'warning',
                content: "请先选择数据！"
            });
            return;
        }
        postData["pk_psndoc"] = pk_psndoc.join(",")
        try {
            let res = await sendemail(postData,1);
            if(res.success) {
                toast({
                    color: 'success',
                    content: '入职信息采集邮件已发送成功'/* 国际化处理： 保存成功*/
                });
               updateState({
                    showTableCheckbox: true,
                    page: 'main'
                });
            } else {
                toast({
                    color: 'error',
                    content: res.message || "请求失败"/* 国际化处理： 保存成功*/
                });
            }
        }
        catch(e) {

        }
    }
    // 发二维码 ---废弃了
    async qrcode() {
        debugger
        let postData = {
        };
        try {
            let res = await sendemail(postData,2);
            if(res.success) {
                this.props.modal.show("yuruzhiQcodeModal");
            } else {
                toast({
                    color: 'error',
                    content: res.message || "请求失败"/* 国际化处理： 保存成功*/
                });
            }
        }
        catch(e) {

        }
    }
}

export default AdjustSort;
